package com.feifan.monthhelper.calendar;

import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.feifan.monthhelper.constant.Constant;
import com.feifan.monthhelper.utils.DateUtils;
import com.feifan.monthhelper.utils.NumberUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.util.Date;

public class CalendarUtils {

    /**
     * 从CalendarView 中返回当前date
     * @param calendarView
     * @return
     */
    public static Date getSelectedDate(CalendarView calendarView) {
        String dateString = calendarView.getSelectedCalendar().getYear() + "-" + calendarView.getSelectedCalendar().getMonth() + "-" + calendarView.getSelectedCalendar().getDay();
        return DateUtils.init().StringToDate(dateString, DateUtils.DateStyle.YYYY_MM_DD);
    }

    /**
     * Calendar 转 date
     * @param calendarView
     * @return
     */
    public static Date getSelectedDate(Calendar calendarView) {
        String dateString = calendarView.getYear() + "-" + calendarView.getMonth() + "-" + calendarView.getDay();
        return DateUtils.init().StringToDate(dateString, DateUtils.DateStyle.YYYY_MM_DD);
    }

    /**
     * 获取当月第一天
     * @return Date
     */
    public static Date getFirtDayOfMonth(Date date) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        clearToDayTime(calendar);
        calendar.set(java.util.Calendar.DAY_OF_MONTH, 1);
        calendar.get(java.util.Calendar.DAY_OF_MONTH);
        LogUtils.d(DateUtils.init().dateToString(calendar.getTime(), DateUtils.DateStyle.YYYY_MM_DD));
        return calendar.getTime();
    }

    /**
     * 获取当月最后一天
     * @return Date
     */
    public static Date getLastDayOfMonth(Date date) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        clearToDayTime(calendar);
        calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));
        calendar.get(java.util.Calendar.DAY_OF_MONTH);
        LogUtils.d(DateUtils.init().dateToString(calendar.getTime(), DateUtils.DateStyle.YYYY_MM_DD));
        return calendar.getTime();
    }

    /**
     * 获取指定日期的偏移日期
     * @return Date
     */
    public static Date getDay(Date date, int offset) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        clearToDayTime(calendar);
        calendar.add(java.util.Calendar.DATE, offset);
        calendar.get(java.util.Calendar.DAY_OF_MONTH);
        LogUtils.d(DateUtils.init().dateToString(calendar.getTime(), DateUtils.DateStyle.YYYY_MM_DD));
        return calendar.getTime();
    }

    /**
     * 日期字符串转date
     * @param dateString 目标日期字符串
     * @return data
     */
    public static java.util.Calendar getCalendarFrom(String dateString) {
        java.util.Calendar selectedDate = java.util.Calendar.getInstance();
        if(!TextUtils.isEmpty(dateString)) {
            String[] strs = dateString.split(Constant.TIME_SPARATOR);
            if(strs.length > 0) {
                selectedDate.set(java.util.Calendar.YEAR, NumberUtils.StringToInt(strs[0]));
            }
            if(strs.length > 1) {
                //month 需要减1
                selectedDate.set(java.util.Calendar.MONTH, NumberUtils.StringToInt(strs[1]) - 1);
            }
            if(strs.length > 2) {
                selectedDate.set(java.util.Calendar.DAY_OF_MONTH, NumberUtils.StringToInt(strs[2]));
            }
        }
        //setTime之后先get一次，让calendar重新计算一次时间，否则不会计算
        selectedDate.get(java.util.Calendar.YEAR);
        return selectedDate;
    }

    /**
     * 计算两个日期之间的差值天数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 相差天数
     */
    public static int getDateSpan(Date startDate, Date endDate) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        clearToDayTime(calendar);
        calendar.setTime(startDate);
        calendar.getTimeInMillis();
        long start = calendar.getTimeInMillis();

        calendar.setTime(endDate);
        calendar.getTimeInMillis();
        long end = calendar.getTimeInMillis();
        return getLongSpan(start, end);
    }

    /**
     * 清空时分秒，毫秒的数据，避免影响时间比较
     * @param calendar 要清空的日历数据
     */
    private static void clearToDayTime(java.util.Calendar calendar) {
        // 时
        calendar.set(java.util.Calendar.HOUR_OF_DAY, 0);
        // 分
        calendar.set(java.util.Calendar.MINUTE, 0);
        // 秒
        calendar.set(java.util.Calendar.SECOND, 0);
        // 毫秒
        calendar.set(java.util.Calendar.MILLISECOND, 0);
    }

    /**
     * 计算两个日期之间的差值天数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 相差天数
     */
    public static int getLongSpan(long startDate, long endDate) {
        return (int) ((endDate - startDate) / 1000 / 3600 / 24);
    }
}
